package com.polaris.live.viewmodel.live

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.constant.LiveConst
import com.polaris.live.manager.AppConfigManager
import com.polaris.live.net.liveService
import com.polaris.live.net.userService
import com.polaris.live.resp.back_resp.CallStartResp
import com.polaris.live.resp.back_resp.CallVo
import com.polaris.live.resp.back_resp.InfoOtherResp
import com.polaris.live.resp.go_resp.CallAnswerBean
import com.polaris.live.resp.go_resp.CallEndBean
import com.polaris.live.resp.go_resp.CallStartBean
import com.polaris.live.resp.go_resp.OtherUserIdBean
import com.polaris.live.utils.BitwiseUtils
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * CallAnswerViewModel
 *
 * @author Created by lucas on 2023/11/14 10:14
 * @since 1.0.0
 */
class CallAnswerViewModel : BaseViewModel() {

    //超时未接听
    val answerTimeoutLiveData = MutableLiveData<Unit>()
    fun launchAnswerTimeout() {
        viewModelScope.launch {
            delay(getDelayMillis())

            answerTimeoutLiveData.value = Unit
        }
    }

    private fun getDelayMillis(): Long {
        return AppConfigManager.getConfig(CommonConst.Configs.callNotAnswerTimeout)
            ?.toLongOrNull()
            ?: LiveConst.ANSWER_TIMEOUT
    }

    //接听还是拒绝
    val answerLiveData = MutableLiveData<ResultState<CallStartResp>>()
    fun answerCall(callId: Long, operation: Int) {
        requestLoading({ liveService.callAnswer(CallAnswerBean(callId, operation)) }, answerLiveData)
    }

    //通话补偿，重新拨打
    val compensationLiveData = MutableLiveData<ResultState<CallStartResp>>()
    fun compensationStart(callStartBean: CallStartBean) {
        request({ liveService.compensationStart(callStartBean) }, compensationLiveData)
    }

    //关注
    val followerLiveData = MutableLiveData<ResultState<Any>>()
    fun addFollower(otherId: Long) {
        requestLoading({ userService.addFollower(OtherUserIdBean(otherId)) }, followerLiveData)
    }

    //获取用户关系信息
    val infoLiveData = MutableLiveData<ResultState<InfoOtherResp>>()
    fun getInfoTies(id: Long) {
        request({
            userService.getInfoOther(
                id,
                BitwiseUtils.bitwiseOperation(
                    longArrayOf(
                        UserManager.BASIC_INFORMATION,
                        UserManager.RELATIONSHIP_INFORMATION
                    )
                )
            )
        }, infoLiveData)
    }

    //通话结束
    val callEndLiveData = MutableLiveData<ResultState<CallVo>>()
    fun callEnd(callId: Long, endType: Int, closeUserId: Long? = null) {
        request({ liveService.callEnd(CallEndBean(callId, endType, closeUserId)) }, callEndLiveData)
    }
}